package com.youthfilmic.yuejian.service;

import com.youthfilmic.yuejian.dao.ApplicationDao;
import com.youthfilmic.yuejian.dao.CommentDao;
import com.youthfilmic.yuejian.dao.SubjectDao;
import com.youthfilmic.yuejian.dao.UserDao;
import com.youthfilmic.yuejian.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by youweixi on 15/10/13.
 */
@Service
public class ApplicationServiceImpl implements ApplicationService {
    @Resource
    private ApplicationDao applicationDao;
    @Resource
    private UserDao userDao;
    @Resource
    private SubjectDao subjectDao;
    @Resource
    private CommentDao commentDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void handleApplication(Long applicationId , Long userId , int status ) throws Exception {
        Application a = applicationDao.findOne(applicationId);
        if (a == null || a.getStatus() != Application.STATUS_WAIT_FOR_HANDLE || a.getStarId() != userId){
            throw new Exception();
        }
        a.setHandleAt(new Date());
        if (status == 1){
            a.setStatus(Application.STATUS_ACCEPT_WAIT_FOR_COMMENT);
            //加1
            subjectDao.incMetCount(a.getSubjectId() , 1);
            //加1
            userDao.incMetCount(a.getStarId() , 1);

            //短信通知用户
            MobileServiceImpl.SendMobileMessageForContent(a.getPhone(), MobileServiceImpl.APPLICATION_MINE_PASS);

        }else if (status == -1){
            a.setStatus(Application.STATUS_DENY);

            //短信通知用户
            MobileServiceImpl.SendMobileMessageForContent(a.getPhone(), MobileServiceImpl.APPLICATION_MINE_DENY);
        }else{
            throw new Exception();
        }
        applicationDao.save(a);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void userCommentForApplication(Long applicationId , Long userId , Comment comment) throws Exception {
        Application a = applicationDao.findOne(applicationId);
        Subject subject = subjectDao.findOne(a.getSubjectId());
        if (a.getUserId() != userId || a.isUserComment()){//不是自己的约见请求
            //跳转回自己的列表
            throw new Exception();
        }
        comment.setPostAt(new Date());
        comment.setSubject(subject);
        comment.setSubjectId(subject.getId());
        comment.setToUser(a.getStar());
        comment.setToUserId(a.getStarId());
        comment.setUser(a.getUser());
        comment.setUserId(a.getUserId());
        commentDao.save(comment);
        a.setUserComment(true);
        a.setUserCommentId(comment.getId());
        //话题评论数加一，用户评价这个话题才有效
        //计算新的平均分
        int e = (subject.getCommentCount() * subject.getEvaluation() + comment.getEvaluation()) / (subject.getCommentCount() + 1);
        subjectDao.incCommentCountAndEvaluation(subject.getId() , 1 , e);
        //注意这里是对star用户进行评论
        userDao.incCommentCount(a.getStarId() , 1);
        //都评论完了，更新状态
        if (a.getStatus() == Application.STATUS_ACCEPT_WAIT_FOR_COMMENT && a.isStarComment()){
            a.setStatus(Application.STATUS_ACCEPT_COMMEND);
        }
        applicationDao.save(a);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void starCommentForApplication(Long applicationId , Long starId , Comment comment) throws Exception {
        Application a = applicationDao.findOne(applicationId);
        Subject subject = subjectDao.findOne(a.getSubjectId());
        if (a.getStarId() != starId || a.isStarComment()){//不是自己的约见请求
            //跳转回自己的列表
            throw new Exception();
        }
        comment.setPostAt(new Date());
        comment.setSubject(subject);
        comment.setSubjectId(subject.getId());
        comment.setToUser(a.getUser());
        comment.setToUserId(a.getUserId());
        comment.setUser(a.getStar());
        comment.setUserId(a.getStarId());
        commentDao.save(comment);
        a.setStarComment(true);
        a.setStarCommentId(comment.getId());

        //star对话题评分不产生影响

        //注意这里是对用户进行评论
        userDao.incCommentCount(a.getUserId() , 1);
        //都评论完了，更新状态
        if (a.getStatus() == Application.STATUS_ACCEPT_WAIT_FOR_COMMENT && a.isUserComment()){
            a.setStatus(Application.STATUS_ACCEPT_COMMEND);
        }
        applicationDao.save(a);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Application makeApplication(Long userId, Long subjectId, Application application) {
        User user = userDao.findOne(userId);
        Subject subject = subjectDao.findOne(subjectId);
        User star = subject.getUser();
        application.setUser(user);
        application.setStar(star);
        application.setDuration(subject.getDuration());
        application.setPostAt(new Date());
        application.setPrice(subject.getPrice());
        application.setStarComment(false);
        application.setUserComment(false);
        application.setSubjectId(subject.getId());
        application.setSubjectTitle(subject.getTitle());
        application.setSubject(subject);
        //等待支付
        application.setStatus(Application.STATUS_WAIT_FOR_PAID);
        application.setUserId(userId);
        application.setStarId(subject.getUserId());
        application.setStarName(subject.getUser().getName());
        applicationDao.save(application);
        return application;
    }
}
